<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class Agent extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'account'=>['require','checkUnique:thinkphp'],
        'phone'=>['require','isMobile' => '/^1[3-9]\d{9}$/','checkPhoneUnique:thinkphp'],
        'name'=>['require'],
        'person'=>['require']
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'account.require'=>'登录账号不能为空',
        'name.require'=>'代理名称不能为空',
        'phone.require'=>'联系电话不能为空',
        'phone.isMobile'=>'手机号格式不正确',
        'person.require'=>'联系人不能为空',
        'account.checkUnique'=>'此账号已被注册'
    ];

    /**
     * @param $value
     * @return bool
     */
    protected function checkUnique($value,$rule, $data = []): bool
    {
        $id='';
        if(isset($data['account_id']) && !empty($data['account_id'])){
            $id= $data['account_id'];
        }
        $service = app('app\service\checkField');
       return  $service->accountCheckUnique($value,$id);
    }

    /**校验手机号是否注册过账号
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     */
    protected function checkPhoneUnique($value,$rule, $data = []): bool
    {
        $id='';
        if(isset($data['account_id']) && !empty($data['account_id'])){
            $id= $data['account_id'];
        }
        $service = app('app\service\checkField');
        return  $service->accountCheckPhone($value,$id);
    }
}
